using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(HMI_Text), true)]
public class HMI_Text_Editor : HMI_EditorBase
{
    private HMI_Text mTarget;

    public override void Awake()
    {
        base.Awake();

        mTarget = target as HMI_Text;
        mTarget.isProgramChange = (mTarget.CompareTag("UI_ControlData"));
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUI.BeginChangeCheck();
        mTarget.isProgramChange = EditorGUILayout.Toggle("程序改变文本：", mTarget.isProgramChange);
        if (EditorGUI.EndChangeCheck())
        {
            mTarget.tag = (mTarget.isProgramChange ? "UI_ControlData" : "Untagged");
            EditorUtility.SetDirty(mTarget);
        }
    }

    protected override string GetDataJson()
    {
        var data = new HMI_TextData();
        data.isProgramChange = mTarget.isProgramChange;

        string json = JsonUtility.ToJson(data, true);
        return json;
    }

    protected override void SetData(string json)
    {
        HMI_TextData data = JsonUtility.FromJson<HMI_TextData>(json);
        mTarget.isProgramChange = data.isProgramChange;

        base.SetData(json);
        EditorUtility.SetDirty(mTarget);
    }
}

[Serializable]
public class HMI_TextData
{
    public bool isProgramChange;
}
